package com.ych.modules.base.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ych.modules.base.dbEntity.TSysCommuniqueEntity;
import com.ych.modules.base.mapper.TSysCommuniqueMapper;
import com.ych.modules.base.service.ITSysCommuniqueService;
import com.ych.modules.common.entity.query.QueryCommuniqueEntity;
import com.ych.utils.TLMap;
import com.ych.utils.UUID;
import com.ych.utils.beanToMap.BeanMapUtil;
import com.ych.utils.mysqlPageUtil.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/3/29
 * Time: 11:13
 * Description:
 */
@Service("ITSysCommuniqueService")
public class TSysCommuniqueServiceImpl extends ServiceImpl<TSysCommuniqueMapper, TSysCommuniqueEntity> implements ITSysCommuniqueService {
    @Override
    public Page<TSysCommuniqueEntity> listCommunique(QueryCommuniqueEntity entity) {
        Wrapper<TSysCommuniqueEntity> wrapper = new EntityWrapper<TSysCommuniqueEntity>()
                .where("1=1")
                .eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId())
                .eq(StringUtils.isNotBlank(entity.getTitle()), "title", entity.getTitle())
                .eq(StringUtils.isNotBlank(entity.getStatus()), "status", entity.getStatus())
                .orderBy("create_time desc");
        Page<TSysCommuniqueEntity> page = this.selectPage(new Query<TSysCommuniqueEntity>(BeanMapUtil.toPageQuery(entity)).getPage(), wrapper);
        return page;
    }

    @Override
    public TSysCommuniqueEntity getCommunique(String id) {
        return this.selectById(id);
    }

    @Override
    public void createCommunique(TSysCommuniqueEntity entity) {
        if (StringUtils.isBlank(entity.getTitle()) || StringUtils.isBlank(entity.getDetail())) {
            throw new RuntimeException("必传参数为空");
        }
        entity.setId(UUID.randomUUID());
        entity.setCreateTime(new Date());
        entity.setCreateUserId(TLMap.getUserId());
        entity.setStatus("0");//正常
        this.insert(entity);
    }

    @Override
    public void negate(String id) {
        TSysCommuniqueEntity entity = this.selectById(id);
        if (null == entity) {
            throw new RuntimeException("公告不存在");
        }
        String status = "0".equals(entity.getStatus()) ? "1" : "0";
        entity.setStatus(status);
        this.updateById(entity);
    }
}
